package com.ivo.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class AuthorityUtil
{
	static public String authorize(String strUser_ID, String strPassword)
	{
		if( strUser_ID == null ) return "User_ID cannot be null. ";
		if( strUser_ID.equals( "" ) == true ) return "User ID should not be empty. ";
		if( strPassword == null ) return "Password cannot be null. ";
		//String strWildPassword = ConnectionFactory.getProperty( "WildPassword" ); 
		//if( strPassword.equals(strWildPassword) == true ) return "OK"; 
		//----------------------------------------------------------------------------------------------------------------------		
		String strXML = "";
		String strMessage = "";
		//----------------------------------------------------------------------------------------------------------------------
		strXML += "<Request>";
		strXML += "<User_ID>" + Util.saveXML( strUser_ID ) + "</User_ID>";
		strXML += "<Password>" + Util.saveXML( strPassword ) + "</Password>";
		strXML += "<Action>AUTHORIZE</Action>";
		strXML += "</Request>";
		//----------------------------------------------------------------------------------------------------------------------
		strMessage = AuthorityUtil.invokeAction( strXML );
		//----------------------------------------------------------------------------------------------------------------------
		return strMessage;
	}

	static public String seekAccount(String strUser_ID)
	{
		if( strUser_ID == null ) return "User_ID cannot be null. ";
		if( strUser_ID.equals( "" ) == true ) return "User ID should not be empty. ";

		String strXML = "";
		String strMessage = "";
		//----------------------------------------------------------------------------------------------------------------------
		strXML += "<Request>";
		strXML += "<User_ID>" + Util.saveXML( strUser_ID ) + "</User_ID>";
		strXML += "<Action>SEEKACCOUNT</Action>";
		strXML += "</Request>";
		//----------------------------------------------------------------------------------------------------------------------
		strMessage = AuthorityUtil.invokeAction( strXML );
		//----------------------------------------------------------------------------------------------------------------------				
		return strMessage;
	}

	static private String invokeAction(String strXML)
	{
		Document objDocument = null;
		Socket objSocket = null;
		SocketAddress objAddress = null;
		BufferedWriter objBufferedWriter = null;
		BufferedReader objBufferedReader = null;
		String strDomainHost = "";
		String strLine = "";
		String strEnd = "#end$";
		String strResultMessage = "";
		//----------------------------------------------------------------------------------------------------------------------
		try {
			strDomainHost = "10.20.2.67";
			//strDomainHost = ConnectionFactory.getProperty( "DomainHost" ); 
			objSocket = new Socket();
			objAddress = new InetSocketAddress( strDomainHost, 8900 );
			objSocket.connect( objAddress );
			strXML = Util.getUTF8HEX( strXML );
			strXML += strEnd;
			//----------------------------------------------------------------------------------------------------------------------
			objBufferedWriter = new BufferedWriter( new OutputStreamWriter( objSocket.getOutputStream() ) );
			objBufferedWriter.write( strXML.toString() );
			objBufferedWriter.flush();
			//----------------------------------------------------------------------------------------------------------------------
			objBufferedReader = new BufferedReader( new InputStreamReader( objSocket.getInputStream() ) );
			strXML = "";
			strLine = objBufferedReader.readLine();
			while( strLine != null ) {
				strXML += strLine;
				strLine = objBufferedReader.readLine();
			} // while( strLine
			strXML = Util.getFromUTF8HEX( strXML );
			objBufferedReader.close();
			objSocket.close();
			//----------------------------------------------------------------------------------------------------------------------		
			objDocument = DocumentHelper.parseText( strXML );
			Element objElement = objDocument.getRootElement();
			//----------------------------------------------------------------------------------------------------------------------		
			if( !objElement.getName().equals( "Response" ) ) throw new WrongTagException( "Response" );
			//----------------------------------------------------------------------------------------------------------------------		
			for( Iterator i = objElement.elementIterator(); i.hasNext(); ) {
				Element objChild = (Element) i.next();
				//----------------------------------------------------------------------------------------------------------------------		
				if( objChild.getName().equals( "ResultMessage" ) ) strResultMessage = Util.restoreXML( objChild.getTextTrim() );
				else throw new WrongTagException( "", "Unknown tag [" + objChild.getName() + "] is found." );
			} // for i		

		} catch( DocumentException doc_ex ) {
			strResultMessage = doc_ex.getMessage();
		} catch( WrongTagException wtag_ex ) {
			strResultMessage = wtag_ex.getMessage();
		} catch( IOException io_ex ) {
			strResultMessage = io_ex.getMessage();
		} // try 
		strResultMessage = strResultMessage.replace( "\n", "" );
		//----------------------------------------------------------------------------------------------------------------------		
		return strResultMessage;
	}
}
